﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace Lypshare.Net
{
	/// <summary>
	/// Packet buffer.
	/// </summary>
    public class PacketBuffer : PacketStream
    {
        // --------------------------------------------------------
        // Class Variables
        // --------------------------------------------------------


        /// <summary>
        /// The _reader.
        /// </summary>
        private BinaryReader _reader = null;


        /// <summary>
        /// The _writer.
        /// </summary>
        private BinaryWriter _writer = null;


        // --------------------------------------------------------
        // Class Functions
        // --------------------------------------------------------


        /// <summary>
        /// Initializes a new instance of the <see cref="Lypshare.Net.PacketBuffer"/> class.
        /// </summary>
        /// <param name='data'>
        /// Data.
        /// </param>
        public PacketBuffer(byte[] data = null)
        {
            _reader = new BinaryReader(this);
            _writer = new BinaryWriter(this);

            if (data != null) 
				_writer.Write(data);

        } // Constructor
			

        /// <summary>
        /// Initializes a new instance of the <see cref="Lypshare.Net.PacketBuffer"/> class.
        /// </summary>
        /// <param name='packet'>
        /// Packet.
        /// </param>
        public PacketBuffer(Packet packet)
        {
            _reader = new BinaryReader(this);
            _writer = new BinaryWriter(this);

            Packet.EncodePacket(packet, this);

        } // Constructor


        /// <summary>
        /// Write the specified value.
        /// </summary>
        /// <param name='value'>
        /// Value.
        /// </param>
        public void Write(String value)
        {
            Write(Encoding.UTF8
                .GetBytes(value));

        } // Write


        /// <summary>
        /// Reads the string.
        /// </summary>
        /// <returns>
        /// The string.
        /// </returns>
        public String ReadString()
        {
            return Encoding.UTF8
                .GetString(ReadBytes());

        } // ReadString


        /// <summary>
        /// Write the specified value.
        /// </summary>
        /// <param name='value'>
        /// Value.
        /// </param>
        public void Write(Int32 value)
        {
            _writer.Write(value);

        } // Write


        /// <summary>
        /// Reads the int32.
        /// </summary>
        /// <returns>
        /// The int32.
        /// </returns>
        public Int32 ReadInt32()
        {
            return _reader.ReadInt32();

        } // ReadInt32


        /// <summary>
        /// Write the specified value.
        /// </summary>
        /// <param name='value'>
        /// Value.
        /// </param>
        public void Write(UInt32 value)
        {
            _writer.Write(value);

        } // Write


        /// <summary>
        /// Reads the U int32.
        /// </summary>
        /// <returns>
        /// The U int32.
        /// </returns>
        public UInt32 ReadUInt32()
        {
            return _reader.ReadUInt32();

        } // ReadUInt32


        /// <summary>
        /// Write the specified value.
        /// </summary>
        /// <param name='value'>
        /// Value.
        /// </param>
        public void Write(bool value)
        {
            _writer.Write(value);

        } // Write


        /// <summary>
        /// Reads the bool.
        /// </summary>
        /// <returns>
        /// The bool.
        /// </returns>
        public bool ReadBool()
        {
            return _reader.ReadBoolean();

        } // ReadBool


        /// <summary>
        /// Write the specified value.
        /// </summary>
        /// <param name='value'>
        /// Value.
        /// </param>
        public void Write(byte[] value)
        {
            Write(value.Length);
            _writer.Write(value);

        } // Write


        /// <summary>
        /// Reads the bytes.
        /// </summary>
        /// <returns>
        /// The bytes.
        /// </returns>
        public byte[] ReadBytes()
        {
            var length = ReadInt32();
            return _reader.ReadBytes(length);

        } // ReadBytes

    } // class PacketBuffer
} // namespace Lypshare.Net